package com.project.communitymanagement.controller;

import com.project.communitymanagement.common.BaseController;
import com.project.communitymanagement.common.Page;
import com.project.communitymanagement.common.ResponseEntity;
import com.project.communitymanagement.entity.TbOwner;
import com.project.communitymanagement.service.TbOwnerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @ProjectName: community-management
 * @Package: com.project.communitymanagement.controller
 * @ClassName: TbOwnerController
 * @Author: "XGP"
 * @Description:
 * @Date: 2022/3/13 12:32
 * @Version: 1.0
 */
@RestController
@CrossOrigin
@RequestMapping("tbowner")
public class TbOwnerController extends BaseController<TbOwnerService, TbOwner> {

    @Autowired
    private TbOwnerService tbownerService;

    @Override
    protected TbOwnerService service() {
        return tbownerService;
    }


    @GetMapping("getAll")
    public ResponseEntity<Page> getAll(){
        return ResponseEntity.success(tbownerService.getAll());
    }

    @PostMapping(value = "/fileUpload")
    public Map fileUpload(@RequestParam(value = "file") MultipartFile file) {
        Map map = new HashMap();
        if (file.isEmpty()) {
            System.out.println("文件为空空");
        }
        String fileName = file.getOriginalFilename();
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        String filePath = "F:\\毕设\\images\\";
//        String filePath = "/usr/local/tomcat/jar/images/";
        fileName = UUID.randomUUID() + suffixName;
        File dest = new File(filePath + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
        map.put("filename", fileName);
        return map;
    }


    @GetMapping(value = "/images",produces = MediaType.IMAGE_JPEG_VALUE)
    public byte[]  images(@RequestParam("fileName") String fileName) throws IOException {
//        File file = new File("/usr/local/tomcat/jar/images/" + fileName);
        File file = new File("F:\\毕设\\images\\" + fileName);
        FileInputStream inputStream = new FileInputStream(file);
        byte[] bytes = new byte[inputStream.available()];
        inputStream.read(bytes, 0, inputStream.available());
        return bytes;
    }

}

